//
//  CategoryDetailViewController.m
//  悦读
//
//  Created by dllo on 15/5/23.
//  Copyright (c) 2015年 贾建兴. All rights reserved.
//

#import "CategoryDetailViewController.h"
#import "SANetWorkingTask.h"
#import "MyFMCategotyDetailCell.h"
#import "BookDetailController.h"
#import "CollectClass.h"
#import "MJRefresh.h"
@interface CategoryDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

// 定义table便于使用
@property(nonatomic, retain)UITableView *table;
// 定义tableArray装请求来的tableview中的所有数据
@property(nonatomic, retain)NSMutableArray *tableArray;
@property(nonatomic, assign) NSInteger i;
@end

@implementation CategoryDetailViewController
- (void)dealloc
{
    [_categoryName release];
    [_cid release];
    [_pxId release];
    [_tableArray release];
    [_table release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // 初始化数组
        self.tableArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTable];
    
    // 设置标题
    self.navigationItem.title = self.categoryName;
    
    if (self.choice == 1) {
        // 判断是否男生女生
        
        
        [self dataRequest];
        
    } else
    {
        [self dataRequestGender];
    }
    
    // 设置导航返回键图片
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backbefore.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action:)];
    self.navigationItem.leftBarButtonItem = button;
    [button release];
    
    [self addHeader];
    [self addFooter];
}
- (void)action:(id)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark 建table
- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    // 不弹
    
    [self.view addSubview:self.table];
    
}

#pragma mark -
#pragma mark 申请数据
- (void)dataRequest
{
    NSString *request = @"http://vip.hentr.com/zhuibook/api/zhuibook/categoryList.do";
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1" forKey:@"version"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:self.cid forKey:@"cId"];
    [SANetWorkingTask request:request parmaters:dic block:^(NSDictionary *result) {
       
        self.tableArray = [[result objectForKey:@"response"] objectForKey:@"ReturnData"];
//        NSLog(@"self.tableArray = %@", self.tableArray);
        [self.table reloadData];
        
        
    }];
}


- (void)dataRequestGender
{
    NSString *request = @"http://vip.hentr.com/zhuibook/api/zhuibook/allBookList.do";
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1" forKey:@"version"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:self.pxId forKey:@"pxId"];
    [SANetWorkingTask request:request parmaters:dic block:^(NSDictionary *result) {
        
        self.tableArray = [[result objectForKey:@"response"] objectForKey:@"ReturnData"];
//        NSLog(@"self.tableArray = %@", self.tableArray);
        [self.table reloadData];
        
        
    }];
}
#pragma mark - 下拉刷新，上拉加载
- (void)addHeader
{

    // 添加下拉刷新头部控件
    [self.table addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
            [self.table reloadData];
            // 结束刷新
            [self.table headerEndRefreshing];
    }];
    //  自动刷新(一进入程序就下拉刷新)
    [self.table headerBeginRefreshing];
}
- (void)addFooter
{
    self.i = 2;
    // 添加上拉刷新尾部控件
    [self.table addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        if (self.choice == 1) {
            NSString *request = @"http://vip.hentr.com/zhuibook/api/zhuibook/categoryList.do";
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:@"1" forKey:@"version"];
            [dic setObject:[NSString stringWithFormat:@"%ld", self.i] forKey:@"page"];
            [dic setObject:self.cid forKey:@"cId"];
            [SANetWorkingTask request:request parmaters:dic block:^(NSDictionary *result) {
                NSDictionary *dic = [result objectForKey:@"response"];
                NSMutableArray *array = [dic objectForKey:@"ReturnData"];
                for (NSString *dic in array) {
                    [self.tableArray addObject:dic];
                }
                self.i++;
                [self.table reloadData];
                [self.table.footer endRefreshing];
            }];
        } else {
            NSString *request = @"http://vip.hentr.com/zhuibook/api/zhuibook/allBookList.do";
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:@"1" forKey:@"version"];
            [dic setObject:[NSString stringWithFormat:@"%ld", self.i] forKey:@"page"];
            [dic setObject:self.pxId forKey:@"pxId"];
            [SANetWorkingTask request:request parmaters:dic block:^(NSDictionary *result) {
                NSDictionary *dic = [result objectForKey:@"response"];
                NSMutableArray *array = [dic objectForKey:@"ReturnData"];
                for (NSString *dic in array) {
                    [self.tableArray addObject:dic];
                }
                self.i++;
                [self.table reloadData];
                [self.table.footer endRefreshing];
            }];
        }
        
        
    }];
}

#pragma mark - table的协议实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIn = @"cell";
    MyFMCategotyDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIn];
    if (!cell) {
        cell = [[MyFMCategotyDetailCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIn];
    }
    
    // 设置点击不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 取出行所对应的数字元素
    NSMutableDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    // 给cell赋值
    [cell addDataToView:dic];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
#pragma mark -
#pragma mark - 点击cell跳转页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
       //跳到书籍详情页面 传id过去
    
    // 点击某行跳到相对应的section所在的视图
    // 需要传值:storyType对应的value传给下一个页面的pxId
    NSMutableDictionary *dic =  [self.tableArray objectAtIndex:indexPath.row];
    
    BookDetailController *detail = [[BookDetailController alloc]init];
    // 传值
   
//    detail.delegate = self;
    
    detail.str = [NSString stringWithFormat:@"%@",
                  [dic objectForKey:@"bookid"]];
    
    // 影藏tabbar, 回来时就又有了
    detail.hidesBottomBarWhenPushed =YES;
    // 跳转页面
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
